package me.hunche.app.web.rest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import me.hunche.app.domain.Activity;
import me.hunche.app.domain.ActivityMember;
import me.hunche.app.domain.Team;
import me.hunche.app.domain.User;
import me.hunche.app.service.ActivityMemberService;
import me.hunche.app.service.ActivityService;
import me.hunche.app.service.CoreService;
import me.hunche.app.service.NotifyService;
import me.hunche.app.service.TeamService;
import me.hunche.app.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/resources")
public class CoreController {
	
	private static final String SUCCESS = "success";

	@Autowired ActivityMemberService memberService;
	@Autowired ActivityService activityService;
	@Autowired NotifyService notifyService;
	@Autowired CoreService coreService;
	@Autowired UserService userService;
	@Autowired TeamService teamService;
	
//	p.activityDate = df.format(dateSpinner.selectedDate);
//	p.reamId = teams.selectedItem.id;
//	p.amount = amount.value;
//	p.least = least.value;
//	p.line = line.text;
//	p.requires = requires.text;
	
//	@RequestMapping(value="/activitys/create")
//	public @ResponseBody Activity createActivity(@RequestBody Activity activity){
//		coreService.crateActivity(activity);
//		return activity;
//	}
	
	@RequestMapping(value="/activitys/create")
	public @ResponseBody Activity createActivity2(
			@RequestParam(value="activityDate") String activityDate,
			@RequestParam(value="teamId") Long teamId,
			@RequestParam(value="amount") Integer amount,
			@RequestParam(value="least") Integer least,
			@RequestParam(value="line") String line,
			@RequestParam(value="requires") String requires
												) throws ParseException{
		Activity activity = new Activity();
		activity.setActivityDate(new SimpleDateFormat("yyyy-MM-dd").parse(activityDate));
		activity.setTeamId(teamId);
		activity.setAmount(amount);
		activity.setLeast(least);
		activity.setLine(line);
		activity.setRequires(requires);
		
		coreService.crateActivity(activity);
		return activity;
	}
	
	@RequestMapping(value="/activitys/{activityId}/join")
	public @ResponseBody String joinActivity(@PathVariable(value="activityId") Long activityId,
			                                   @RequestParam(value = "userIds") String userIds,
			                                   @RequestParam(value = "needNotify",defaultValue="true",required=false) Boolean needNotify){
		List<User> us = userService.findByIds(userIds);
		coreService.joinActivity(us, activityId,needNotify);
		return "success";
	}
	
	
	@RequestMapping(value="/activitys/{activityId}/apply")
	public @ResponseBody String applyActivity(@PathVariable(value="activityId") Long activityId,
			                                   @RequestParam(value = "mobile") String mobile,
			                                   @RequestParam(value = "needNotify",defaultValue="true",required=false) Boolean needNotify){
		coreService.joinActivity(mobile, activityId,needNotify);
		return "success";
	}
	
	@RequestMapping(value="/activitys/{activityId}/recruit")
	public @ResponseBody String activityRecruit(@PathVariable(value="activityId") Long activityId,@RequestParam(value="userIds") String userIds){

//		Activity a = activityService.findActivityById(activityId);
//		if(a!=null){
//			notifyService.recruit(a);
			coreService.recruit(activityId,userIds);
//		}
		return "success";
	}
	
	@RequestMapping(value="/activitys/recruits")
	public @ResponseBody List<Activity> findRecruitActivitys(){
		List<Activity> list = activityService.findRecruitActivitys();
		if(list!=null && !list.isEmpty()){
			for(Activity a:list){
				a.setMembers(memberService.findByActivityId(a.getId()));
			}
		}
		
		return list;
	}
	
	@RequestMapping(value="/activitys/list")
	public @ResponseBody List<Activity> list(){
		return  activityService.findRecruitActivitys();
	}
	
	@RequestMapping(value="/activitys/{activityId}/confirm")
	public @ResponseBody String confirmActivity(@PathVariable(value="activityId") Long activityId,
												@RequestParam(value="leaderId") Long leaderId, 
												@RequestParam(value="memberIds") String memberIds){
//		return amservice.findByActivityId(activityId);
		String[] array = memberIds.split(",");
		
		Long[] ms = new Long[array.length];
		for(int i=0;i<array.length;i++){
			ms[i] = Long.valueOf(array[i]);
		}
		
		coreService.confirmActivity(activityId, leaderId, ms);
		
		return "success";
	}
	
	@RequestMapping(value="/activitys/{activityId}/members")
	public @ResponseBody List<ActivityMember> findMemmberByActivityId(@PathVariable(value="activityId") Long activityId){
		return memberService.findByActivityId(activityId);
	}
	
	@RequestMapping(value="/activitys/{activityId}")
	public @ResponseBody Activity getActivityById(@PathVariable(value="activityId") Long activityId){
		return activityService.findActivityById(activityId);
	}
	
	@RequestMapping(value="/activitys")
	public @ResponseBody List<Activity> getLatestActivitys(@RequestParam(value="teamId",required=false) Long teamId,
														   @RequestParam(value="max_id",required=false) Long maxId,
														   @RequestParam(value="since_id",required=false) Integer sinceId,
														   @RequestParam(value="count",defaultValue="10",required=false) Integer count
														   ) {
//		return activityService.findByPage(teamId, maxId, sinceId, count);
		return activityService.getAll();
		
	}
	
	@RequestMapping(value="/activitys/latest{number}")
	public @ResponseBody List<Activity> getLatestActivitys(@PathVariable(value="number") Integer number,
			                                               @RequestParam(value="teamId") Long teamId) {
		
		//TODO
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Date sdata = sdf.parse(activityDate);
//		return activityService.findByActivityDateAndStatus(sdata, Activity.STATUS_READY);
//		PageInfo p = new PageInfo();
//		p.setCountOfCurrentPage(10);
//		PageInfo p2 = activityService.query(p, null);
//		return p2.getPageResults();
		
		List<Activity> list = activityService.getLatestActivitys(teamId, number);
		
		return list;
	}
	
//	@RequestMapping(value="/activity/list")
//	public @ResponseBody List<Activity> findVaildActivityByActivityDate(@RequestParam(value = "activityDate") String activityDate) throws ParseException{
//		
//		//TODO
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Date sdata = sdf.parse(activityDate);
//		return activityService.findByActivityDateAndStatus(sdata, Activity.STATUS_READY);
//	}
	
	@RequestMapping(value="/activitys/{activityId}/remind")
	public @ResponseBody Activity activityRemind(@PathVariable(value="activityId") String activityId){
		Activity a =  activityService.findActivityById(Long.valueOf(activityId));
		notifyService.remind(a);
		return a;
	}
	
	@RequestMapping(value="/activitys/{activityId}/ready")
	public @ResponseBody Activity activityReady(@PathVariable(value="activityId") String activityId){
		Activity a =  activityService.findActivityById(Long.valueOf(activityId));
		notifyService.ready(a);
		return a;
	}
	
	
	@RequestMapping(value="/members/{memberId}/gone")
	public @ResponseBody String gone(@PathVariable(value="memberId") Long memberId){
		ActivityMember m = memberService.get(memberId);
		memberService.gone(m);
		return SUCCESS;
	}
	
	@RequestMapping(value="/members/{memberId}/ready")
	public @ResponseBody String ready(@PathVariable(value="memberId") Long memberId){
		ActivityMember m = memberService.get(memberId);
		memberService.ready(m);
		return SUCCESS;
	}
	
	@RequestMapping(value="/teams/{teamId}/members")
	public @ResponseBody List<User> findTeamMembers(@PathVariable(value="teamId") Long teamId){
		List<User> us = userService.findByTeamId(teamId);
		return us;
	}
	
	
	@RequestMapping(value="/teams")
	public @ResponseBody List<Team> findTeams(@RequestParam(value="userId",required=false,defaultValue="101") Long userId){
		List<Team> ts = teamService.findByUserId(userId);
		return ts;
	}
	
}	